package Array.study;

import java.util.Scanner;

public class CirceQueue {
    public static void main(String[] args) {
        Cirdequeue cirdequeue = new Cirdequeue(3);
        boolean loop=true;
        while (loop) {
            System.out.println("a（addQueue）,增加数值");
            System.out.println("g(getQueue),获取数值");
            System.out.println("s(showQueue),展示数值");
            System.out.println("e(exit),退出");
            System.out.println("h（head），展示头个数值");
            Scanner scanner=new Scanner(System.in);
            String key = scanner.next();
            switch (key) {
                case "a":
                    System.out.println("输入你要增加的数值：");
                    int n = scanner.nextInt();
                    cirdequeue.addQueue(n);
                    break;
                case "g":
                    System.out.println("数值："+cirdequeue.getQueue());
                    break;
                case "s":
                    try {
                        cirdequeue.showQueue();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "e":
                    System.exit(0);
                    loop=false;
                    break;
                case "h":
                    cirdequeue.showhead();
                    break;
                default:
                    System.out.println("输入有误");
                    break;
            }

        }
    }
}
class Cirdequeue{
    private int maxSize;
    private int[] arr;
    private int front;
    private int rear;

    public Cirdequeue(int maxSize) {
        this.maxSize = maxSize;
        arr=new int[this.maxSize];
    }

    public boolean isfull(){
        return (rear+1)%maxSize==front;//满了
    }
    public boolean isnull(){
        return rear==front;//空
    }
    public void addQueue(int n){
        if (isfull()){

            System.out.println("满了");
            return;
        }
        arr[rear]=n;
        rear=(rear+1)%maxSize;
    }
    public int getQueue(){
        if (isnull()){
            throw new RuntimeException("数组为空");
        }
        int values=arr[front];
        front=(front+1)%maxSize;
        return values;
    }
    public void showQueue(){
        if (isnull()){
            throw new RuntimeException("数组为空");
        }
        for (int i = front; i <front+Size(); i++) {
            System.out.println(i);
            System.out.println("arr[%d]="+arr[i]);
        }
    }
    public void showhead(){
        if (isnull()){
            throw new RuntimeException("数组为空");
        }
        System.out.println(arr[front]);
    }
    public int Size(){//有效个数
        return (rear+maxSize-front)%maxSize;
    }
}
